clearvars -except Globaloption option

%% Saving parameter estimates to automatically update the paper
fID = fopen(['../../results/tables_main/US/pre1946/TablePsi_pre_us.tex'], 'w');

%% Baseline Results

loaddata_4CIjoint_annual;

% Psi
Psichar = ['a'; 'b'; 'c'; 'd'; 'e'; 'f'; 'g'; 'h'; 'i'; 'j'];
namebegin = '\\newcommand{\\preusPsi';
nameend1 = '}{%4.2f}\n';
nameend2 = '}{%4.0f}\n';

for ii = 1:N

    for jj = 1:N
        fprintf(fID, [namebegin Psichar(ii) Psichar(jj) nameend1], Psi(ii, jj));
    end

end

% maxeig and R2
fprintf(fID, '\\newcommand{\\preusmaxeig}{%4.3f}\n', max(abs(eig(Psi))));
fprintf(fID, '\\newcommand{\\preusRsqra}{%4.1f}\n', 100 * R2(1));
fprintf(fID, '\\newcommand{\\preusRsqrb}{%4.1f}\n', 100 * R2(2));
fprintf(fID, '\\newcommand{\\preusRsqrc}{%4.1f}\n', 100 * R2(3));
fprintf(fID, '\\newcommand{\\preusRsqrd}{%4.1f}\n', 100 * R2(4));
fprintf(fID, '\\newcommand{\\preusRsqre}{%4.1f}\n', 100 * R2(5));
fprintf(fID, '\\newcommand{\\preusRsqrg}{%4.1f}\n', 100 * R2(6));
fprintf(fID, '\\newcommand{\\preusRsqrh}{%4.1f}\n', 100 * R2(7));
fprintf(fID, '\\newcommand{\\preusRsqrj}{%4.1f}\n', 100 * R2(9));

% T stat
Tstatchar = Psichar;
tnamebegin = '\\newcommand{\\preusTstatt';
tnameend1 = '}{%4.2f}\n';
tnameend2 = '}{%4.0f}\n';

for ii = 1:N

    for jj = 1:N
        fprintf(fID, [tnamebegin Tstatchar(ii) Tstatchar(jj) tnameend1], tstat(ii, jj));
    end

end

% Sig
Sigchar = Psichar;
snamebegin = '\\newcommand{\\preusSig';
snameend1 = '}{%4.2f}\n';
snameend2 = '}{%4.0f}\n';

for ii = 1:N

    for jj = 1:N
        fprintf(fID, [snamebegin Sigchar(ii) Sigchar(jj) snameend1], 100 * Sig(ii, jj));
    end

end

% X2 mean
fprintf(fID, '\\newcommand{\\preusVarMeana}{%4.2f}\n', 100 * pi0);
fprintf(fID, '\\newcommand{\\preusVarMeanb}{%4.2f}\n', 100 * y0nom_1);
fprintf(fID, '\\newcommand{\\preusVarMeanc}{%4.2f}\n', 100 * yspr0);
fprintf(fID, '\\newcommand{\\preusVarMeand}{%4.2f}\n', 100 * x0);
fprintf(fID, '\\newcommand{\\preusVarMeane}{%4.2f}\n', 100 * mean(divgrm));
fprintf(fID, '\\newcommand{\\preusVarMeanf}{%4.2f}\n', mean(exp(pdm)));
fprintf(fID, '\\newcommand{\\preusVarMeanh}{%4.2f}\n', 100 * mean(deltalogtau));
fprintf(fID, '\\newcommand{\\preusVarMeani}{%4.2f}\n', 100 * mean((taxrevgdp)));
fprintf(fID, '\\newcommand{\\preusVarMeanj}{%4.2f}\n', 100 * mean(deltalogg));
fprintf(fID, '\\newcommand{\\preusVarMeank}{%4.2f}\n', 100 * mean((spendgdp)));

load MAT/USprepara.mat

load MAT/benchmark_US_before1946.mat

fprintf(fID, '\\newcommand{\\preuskzerox}{%4.2f}\n', k0x);
fprintf(fID, '\\newcommand{\\preuskonex}{%4.2f}\n', k1x);
fprintf(fID, '\\newcommand{\\preusmeanpvsurplus}{%4.2f}\n', mean(s));
fprintf(fID, '\\newcommand{\\preusmeansurpluszeroz}{%4.2f}\n', mean(upper));
fprintf(fID, '\\newcommand{\\preusmeanpvsurpluspct}{%4.2f}\n', 100 * mean(s));
fprintf(fID, '\\newcommand{\\preusmeansurpluszerozpct}{%4.2f}\n', 100 * mean(upper));
fprintf(fID, '\\newcommand{\\preuspdgdp}{%4.2f}\n', mean(exp(pxbar)));
fprintf(fID, '\\newcommand{\\preusgdpreturn}{%4.2f}\n', 100 * mean(gdpreturn));
fprintf(fID, '\\newcommand{\\preusdebtgdp}{%4.2f}\n', nanmean(gdebt));
fprintf(fID, '\\newcommand{\\preusdebtgdppct}{%4.2f}\n', 100 * nanmean(gdebt));

fprintf(fID, '\\newcommand{\\preuspvstodebt}{%4.2f}\n', 100 * nanmean(s) / nanmean(gdebt));

fprintf(fID, '\\newcommand{\\preussurpluspct}{%4.2f}\n', 100 * (mean(taxrevgdp) - mean(spendgdp)));
A = corrcoef([gdebt(2:end) s'], 'rows', 'pairwise');

fprintf(fID, '\\newcommand{\\preuscorrpvsdebt}{%4.2f}\n', A(2));
fprintf(fID, '\\newcommand{\\preusuppertodebt}{%4.2f}\n', 100 * upper / nanmean(gdebt));

%% Close File
fclose(fID);
